/*
 * grunt-wp-git-to-svn
 * https://github.com/stephenharris/wp-git-to-svn
 *
 * Copyright (c) 2013 Stephen Harris
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {

	var exec = require('child_process').exec;
	var inquirer = require('inquirer');
//        var chalk = require('chalk');
        var _ = grunt.util._;


  // Please see the Grunt documentation for more information regarding task
  // creation: http://gruntjs.com/creating-tasks

  grunt.registerMultiTask('wp_git_to_svn', 'Deploys a git Repo to the WordPress SVN repo', function() {

	var cb = this.async();
                var options = this.options({
                        stdout: true,
                        stderr: false,
                        failOnError: false
                });

	//var myTerminal = require("child_process").exec,
	//var cmd = 'deploy.sh';
	var cmd = 'echo hello';

	var questions = [
		{
		    type: "input",
		    name: "plugin_slug",
		    message: "What's your plugin name"
	  },
	{
		    type: "input",
		    name: "svn_username",
		    message: "What's your SVN username"
	  }

	];

	inquirer.prompt( questions, function( answers ) {
		
		var slug = answers.plugin_slug;
		var mainfile = slug+".php";
		var svnpath = "/tmp/" + slug;
		var svnuser = "stephenharris"; 
		var svnurl = "http://plugins.svn.wordpress.org/" + slug;


		/*
		# Check version in readme.txt is the same as plugin file
		NEWVERSION1=`grep "^Stable tag" $GITPATH/readme.txt | awk -F' ' '{print $3}'`
		echo "readme version: $NEWVERSION1"
		NEWVERSION2=`grep "^Version" $GITPATH/$MAINFILE | awk -F' ' '{print $2}'`
		echo "$MAINFILE version: $NEWVERSION2"
 
		if [ "$NEWVERSION1" != "$NEWVERSION2" ]; then echo "Versions don't match. Exiting...."; exit 1; fi
		*/

		grunt.log.writeln( 'Checking out '+ svnurl );
		var cmd = 'echo test';
		var cmd = 'svn co '+svnurl+ ' ' + svnpath;
		var cp = exec(cmd,{}, function(){
			grunt.log.writeln( 'Check out complete');
			grunt.log.writeln( process.cwd());


			grunt.log.writeln( 'Ignoring github specific files and deployment script');
			cmd = 'svn propset svn:ignore "deploy.sh readme.md .git .gitignore" "'+svnpath+'/trunk/"';
			//var cp = exec(cmd);

			//export git -> SVN
			grunt.log.writeln( 'Exporting the HEAD of master from git to the trunk of SVN');
			cmd = 'git checkout-index -a -f --prefix='+svnpath+'/trunk/';
			//var cp = exec(cmd);

			//TODO submodules?
			cmd = 'cd '+svnpath+'/trunk\n svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk \'{print $2}\' | xargs svn add';
			var cp = exec(cmd,{}, function( a, b, c ){
				grunt.log.writeln( a );
				grunt.log.writeln( b );
				grunt.log.writeln( c );

			});
 
			//echo "Changing directory to SVN and committing to trunk"
			//cd $SVNPATH/trunk/
			//# Add all new files that are not set to be ignored
			//svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}' | xargs svn add
			//#svn commit --username=$SVNUSER -m "$COMMITMSG"
 
			//echo "Creating new SVN tag & committing it"
			//cd $SVNPATH
			//svn copy trunk/ tags/$NEWVERSION1/
			//cd $SVNPATH/tags/$NEWVERSION1
			//#svn commit --username=$SVNUSER -m "Tagging version $NEWVERSION1"
 
			//echo "Removing temporary directory $SVNPATH"
			//#rm -fr $SVNPATH/
 
			//echo "*** FIN ***"
		});




		

	});


	



  });

};
